<template>
  <div class="in-wrap">
    <!-- 公共头引入 -->
    <header id="header">
      <section class="container">
        <h1 id="logo">
          <a href="#" title="扶摇学院">
            <img src="~/assets/img/logo.png" width="100%" alt="扶摇学院" />
          </a>
        </h1>
        <div class="h-r-nsl">
          <ul class="nav">
            <router-link to="/" tag="li" active-class="current" exact>
              <a>首页</a>
            </router-link>
            <router-link to="/course" tag="li" active-class="current">
              <a>课程</a>
            </router-link>
            <router-link to="/teacher" tag="li" active-class="current">
              <a>讲师</a>
            </router-link>
          </ul>
          <ul class="h-r-login">
            <li v-if="!loginInfo.id" id="no-login">
              <a href="/login" title="登录">
                <em class="icon18 login-icon">&nbsp;</em>
                <span class="vam ml5">登录</span>
              </a>
              |
              <a href="/register" title="注册">
                <span class="vam ml5">注册</span>
              </a>
            </li>
            <li v-if="loginInfo.id" id="is-login-one" class="mr10">
              <el-badge v-bind:is-dot="haveNewMsg" class="msg">
                <a id="headerMsgCountId" href="/message" title="消息">
                  <span class="aliconfont icon-xiaoxi2"></span>
                </a>
              </el-badge>
            </li>
            <li v-if="loginInfo.id" id="is-login-two" class="h-r-user">
              <nuxt-link to="/ucenter/myInfo" title>
                <el-popover
                  placement="top-start"
                  width="200"
                  trigger="hover"
                  >
                  <span id="userName" class="vam disIb">{{
                      loginInfo.nickname
                    }}</span><br/>
                  <a
                    href="javascript:void(0);"
                    title="退出"
                    @click="logout()"
                    class="ml5"
                  >退出</a
                  >
                  <img slot="reference"
                    :src="loginInfo.avatar"
                    width="30"
                    height="30"
                    class="vam picImg"
                    alt
                  />
                </el-popover>

              </nuxt-link>
            </li>
            <!-- /未登录显示第1 li；登录后显示第2，3 li -->
          </ul>
          <aside class="h-r-search">
              <label class="h-r-s-box">
                <el-input
                  placeholder="请输入课程"
                  v-model="aboutCourse">
                </el-input>
                <button type="submit" class="s-btn" @click="queryCourse()">
                  <em class="icon18">&nbsp;</em>
                </button>
              </label>
          </aside>
        </div>
        <aside class="mw-nav-btn">
          <div class="mw-nav-icon"></div>
        </aside>
        <div class="clear"></div>
      </section>
    </header>
    <!-- /公共头引入 -->

    <nuxt />
  </div>
</template>
<script>
import "~/assets/fonticon/iconfont.css";
import "~/assets/css/reset.css";
import "~/assets/css/theme.css";
import "~/assets/css/global.css";
import "~/assets/css/web.css";
import "~/assets/css/base.css";
import "~/assets/css/activity_tab.css";
import "~/assets/css/bottom_rec.css";
import "~/assets/css/nice_select.css";
import "~/assets/css/order.css";
import "~/assets/css/swiper-3.3.1.min.css";
import "~/assets/css/pages-weixinpay.css";

import cookie from "js-cookie";
import loginApi from "@/api/login";
import msgApi from "@/api/message";

export default {
  data() {
    return {
      token: "",
      loginInfo: {
        id: "",
        age: "",
        avatar: "",
        mobile: "",
        nickname: "",
        sex: ""
      },
      aboutCourse: "",
      haveNewMsg: false
    };
  },

  created() {
    // this.token = this.$route.query.token;
    // if (this.token) {
    //   this.wxLogin();
    // } else {
    //   this.showInfo();
    // }
    this.showInfo();
  },

  methods: {
    //查询课程
    queryCourse() {
      cookie.set("aboutCourse",this.aboutCourse);
      // localStorage.setItem("aboutCourse",this.aboutCourse);
      this.$router.push("/course");
    },
    //微信登录
    wxLogin() {
      cookie.set("fuyao_token", this.token, {
        domain: "localhost"
      });
      cookie.set("fuyao_ucenter", "", { domain: "localhost" });
      loginApi.getLoginInfo().then(response => {
        this.loginInfo = response.data.data.item;
        cookie.set("fuyao_ucenter", this.loginInfo, { domain: "localhost" });
      });
    },
    getNewMsg() {
      msgApi.getNewMsg().then(response => {
        this.haveNewMsg = response.data.data.result;
      });
    },

    showInfo() {
      //debugger
      var jsonStr = cookie.get("fuyao_ucenter");
      if (jsonStr) {
        console.log("jsonStr" + JSON.stringify(jsonStr));
        this.loginInfo = JSON.parse(jsonStr);
        this.getNewMsg();
      }
    },

    logout() {
      //debugger
      cookie.set("fuyao_ucenter", "", { domain: "localhost" });
      cookie.set("fuyao_token", "", { domain: "localhost" });

      //跳转页面
      window.location.href = "/";
    }
  }
};
</script>
